<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="请输入用户名"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="请输入密码"
				></wInput>
				<wButton
					class="wbutton"
					text="登 录"
					:rotate="isRotate" 
					@click="startLogin"
				></wButton>
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	import Cookies from 'js-cookie';
	import store from '@/store/store.js';
	import config from '@/utils/config.js';
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage:'../../static/common/icon/logo1.png',
				phoneData:'', //用户名
				passData:'', //密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			this.isLogin();
		},
		methods: {
			isLogin(){
				// 判断缓存中是否登录过，直接登录
				try {
					const value = uni.getStorageSync('setUserData');
					if (value) {
						//有登录信息
						console.log("已登录用户：",value);
						_this.$store.dispatch("setUserData",value); //存入状态
						uni.reLaunch({
							url: '../../../pages/index',
						});
					}
				} catch (e) {
					// error
				}
			},
		    startLogin(e){
                console.log(e)
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.passData.length < 5) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return;
		        }
				
				uni.showLoading({
				        title: '登录中'
				    });
				    uni.request({
				        url: `${config.apiBaseUrl}/user/login`, // 后端登录接口地址
				        method: 'POST', 
				        data: {
				            username: this.phoneData,
				            password: this.passData
				        },
				        success: (res) => {
				            uni.hideLoading();
				            
				            if (res.data.data.token != null && res.data.code == 1) {
				                let userdata = {
				                    "username": res.data.data.username,
				                    "accesstoken": res.data.data.token
				                }; 
							
								// 使用Cookie保存token，这里设置过期时间为7天
								Cookies.set('token', res.data.data.token, { expires: 7 }); 
								_this.$store.dispatch("setUserData", userdata); 
								
				                uni.showToast({
				                    icon: 'success',
				                    position: 'bottom',
				                    title: '登录成功'
				                });
				                uni.reLaunch({
				                    url: '../../pages/index/index', // 登录成功后跳转到指定界面
				                });
				            } else {
				                _this.passData = "";
				                uni.showToast({
				                    icon: 'error',
				                    position: 'bottom',
				                    title: '账号或密码错误'
				                });
				            }
				        },
				        fail: (err) => {
				            uni.hideLoading();
				            uni.showToast({
				                icon: 'error',
				                position: 'bottom',
				                title: '登录失败，请稍后重试'
				            });
				        }
				    });
				
				    _this.isRotate = true;
				    setTimeout(function () {
				        _this.isRotate = false;
				    }, 3000);
				
		    }
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>